﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obligatorio2013.Clases;
using Obligatorio2013.Modelo;

namespace Obligatorio2013.Pages.Private
{
    public partial class AdForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((bool)Session["logged"] == true)
            {
               
                if (!Page.IsPostBack)
                {
                    this.showAdTypes();
                    this.calendarFrom.SelectedDate = System.DateTime.Today;
                    this.calendarTo.SelectedDate = System.DateTime.Today;
                    if (Session["controller"] == null)
                    {
                        Session["controller"] = Sistema.Instance;
                    }
                }
            }
            else
            {
                Response.Redirect("~/Pages/Private/LoginForm.aspx");
            }
        }



        private void showAdTypes()
        {
            Array types = Enum.GetValues(typeof(Advertisement.type));

            foreach (int key in types)
            {
                string desc = Enum.GetName(typeof(Advertisement.type), key);
                this.DropDownList1.Items.Add(new ListItem(desc, key.ToString()));
            }
        }

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            
            string type = this.DropDownList1.SelectedValue;
            double price = Sistema.Instance.adCostCaluclate(this.txtTitle.Text, this.txtDescription.Text, type, this.calendarFrom.SelectedDate.Value, this.calendarFrom.SelectedDate.Value);
            this.txtCost.Text = price.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string usr = (string)(Session["user"]);
            double price = Double.Parse(this.txtCost.Text);
            string type = Enum.GetName(typeof(Advertisement.type), this.DropDownList1.SelectedValue);

            bool ret = Sistema.Instance.saveAd(this.txtTitle.Text, this.txtDescription.Text,type, this.adImage.FileName,
                this.calendarFrom.SelectedDate.Value, this.calendarFrom.SelectedDate.Value, this.txtContact.Text, this.txtPhone.Text, price, usr);
            if (ret)
            {
                uploadImg();                
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/UserList.aspx", true);
        }


        protected void uploadImg() 
        {
            this.adImage.SaveAs(Server.MapPath("~") + "/Pages/Public/Images/" + this.adImage.FileName);
            this.adImageView.ImageUrl = "../Public/Images/" + this.adImage.FileName;
        }


        protected void DateValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = (args.Value != null);
        }
    }
}